/*绑定回车到查询按钮*/
$(function(){
	$(document).keydown(function(){ 
		if(event.keyCode==13){ 
		$("#chaxtj-hclb-chaxunbtn").trigger('click'); 
		return false;
		}
	});
});
var kaishisj = $('#ylhc-kaishjsj').val();
var jieshusj = $('#ylhc-jieshusj').val();
var mingcheng = '';
var yewulx = '';
//查询按钮
$('#chaxtj-hclb-chaxunbtn').click(function(){
    mingcheng = $("#chaxtj-hclb-mingcheng").val();
    kaishisj = $('#ylhc-kaishjsj').val();
    jieshusj = $('#ylhc-jieshusj').val();
    yewulx= $('#chaxtj-hclb-ywrkcanshu').val();
    if(yewulx==0){
    	yewulx='';
    }
    var lxcanshu=$('#chaxtj-hclb-lxcanshu').val();
    if(lxcanshu==1){
    	$("#chaxtj-haocaihzlbnr").css("display","none");
    	getHaocaiMx();
    }else if(lxcanshu==2){
    	$("#chaxtj-haocaimxlbnr").css("display","none");
    	getHaocaiHz();
    }
});
//点击选项取值
$('.ylhc-clickcontroller-div ul li').click(function(){
    var bValue = $(this).val();
    $(this).parent().siblings(".ylhc-clickcontroller-input").val(bValue);
    mingcheng= $("#chaxtj-hclb-mingcheng").val();
    kaishisj = $('#ylhc-kaishjsj').val();
    jieshusj = $('#ylhc-jieshusj').val();
    yewulx=$('#chaxtj-hclb-ywrkcanshu').val();
    if(yewulx==0){
    	yewulx='';
    }
    var lxcanshu=$('#chaxtj-hclb-lxcanshu').val();
    $(this).addClass('xuanzong').siblings('li').removeClass('xuanzong');
    if(lxcanshu==1){
    	getHaocaiMx();
    }else if(lxcanshu==2){
    	getHaocaiHz();
    }
});

//初始时间获取方法
function getShijian(prekaishisj,prejieshusj){
		//重置按钮
		$("#chaxtj-hclb-chongzhibtn").click(function(){
		$("#chaxtj-hclb-mingcheng").val("");
		$('#ylhc-kaishjsj').val(prekaishisj);
		$('#ylhc-jieshusj').val(prejieshusj);
		$('#chaxtj-hclb-ywrkcanshu').val(0);
		$('#chaxtj-hclb-lxcanshu').val(1);
		$("#chaxtj-haocaihzlbnr").css("display","none");
		//重置状态栏样式控制
		$('#yeulx-quanbu').addClass('xuanzong').siblings('li').removeClass('xuanzong');
		$('#lx-haocaimx').addClass('xuanzong').siblings('li').removeClass('xuanzong');
		//重置后进行查询
		if(prekaishisj!=null){
			kaishisj=prekaishisj;
			jieshusj=prejieshusj;
			mingcheng = '';
			yewulx = $('#chaxtj-hclb-ywrkcanshu').val();
			if(yewulx==0){
    			yewulx='';
    		}
			getHaocaiMx();
		}
});
//		首次进入查询
		if(prekaishisj!=null){
			kaishisj=prekaishisj;
			jieshusj=prejieshusj;
			mingcheng = '';
			yewulx = '';
			getHaocaiMx();
		}
}

function getHaocaiMx(){
	var totalNumber = 0;
	var params={
			'kaishisj':kaishisj,
	        'jieshusj':jieshusj,
	        'mingcheng':mingcheng,
	        'yewulx':yewulx
		};
		$.ajax({
		    type:"POST",
		    url:"../chaxuntjylhc/getYlhcMxCount",
		    data:params,
		    async:false,
		    success: function(data){
		    	if(data!=null) {
		    		totalNumber = data;
				}
		    	if(totalNumber==0){
					$("#chaxtj-haocaimxlbnr").css("display","none");
					$("#chaxtj-hclb-img").html("<img src='"+path+"/image/chaxtj/cxtj-nobody.png'>");
				}else{
					$("#chaxtj-hclb-img").html("");
					$("#chaxtj-haocaihzlbnr").css("display","none");
					$("#chaxtj-haocaimxlbnr").css("display","block");
				}
			}
		});
	function createHaocaiMx(name) {
		$('#tb-haocaimx').html('');
	  var container = $('#pagination-'+name);
	  container.pagination({
	    dataSource:'../chaxuntjylhc/getYlhcMxByPage?kaishisj='+kaishisj+'&jieshusj='+jieshusj+'&mingcheng='+mingcheng+'&yewulx='+yewulx,
	    locator: 'entities',
	    pageSize: 12,
	    totalNumber: totalNumber,
		showNavigator: true,
   	    formatNavigator: '共 <%= totalNumber %> 条',
	    callback: function(data, pagination) {
	    	if(data!='') {
				var content = '';
				$(data).each(function(index,e){
					//销售数量处理
					if(this.xiaoshousl!=null&&this.danwei!=null){
						var shuliang = this.xiaoshousl + this.danwei;
					}else if(this.xiaoshousl!=null){
						var shuliang=this.xiaoshousl;
					}
					//业务类型处理
					var yewulx = this.laiyuan;
					if(yewulx==1) {
						yewulx = '微诊疗';
					}else if(yewulx==2) {
						yewulx = '售药';
					}else if(this.laiyuan==3) {
						yewulx = '常规门诊';
					} else if(yewulx == 4) {
						yewulx = '库存调整';
					}
					//厂商处理
					var changshang;
					if(this.changshang!=null){
						changshang=this.changshang;
					}else{
						changshang='';
					}
					//规格处理
					var guige;
					if(this.guige!=null){
						guige=this.guige;
					}else{
						guige='';
					}
					content += '<tr>'+
	                '<td width="5%">'+(((pagination.pageNumber-1)*pagination.pageSize)+index+1)+'</td>'+
	                '<td width="20%" class="left">'+this.mingcheng+'</td>'+
	                '<td width="15%">'+guige+'</td>'+
	                '<td width="17%" class="left">'+changshang+'</td>'+
	                '<td width="10%">'+this.xiaoshourq+'</td>'+
	                '<td width="10%">'+yewulx+'</td>'+
	                '<td width="10%">'+shuliang+'</td>'+
	                '<td width="13%" class="right">'+parseFloat(this.xiaoshouje).toFixed(2)+'</td>'+
	                '</tr>';
				});
				$('#tb-haocaimx').html(content);
	    	}
	    }
	  });
	  return container;
	}
	createHaocaiMx('fenyylhcmx');
	if(totalNumber <= 12) {
		$('#pagination-fenyylhcmx').css('display','none');
	}else{
		$('#pagination-fenyylhcmx').css('display','block');
	}
}


function getHaocaiHz(){
	
	var totalNumber = 0;
	var params={
			'kaishisj':kaishisj,
	        'jieshusj':jieshusj,
	        'mingcheng':mingcheng,
	        'yewulx':yewulx
		};
		$.ajax({
		    type:"POST",
		    url:"../chaxuntjylhc/getYlhcHzCount",
		    data:params,
		    async:false,
		    success: function(data){
		    	if(data!=null) {
		    		totalNumber = data;
				}
		    	if(totalNumber==0){
					$("#chaxtj-haocaihzlbnr").css("display","none");
					$("#chaxtj-hclb-img").html("<img src='"+path+"/image/chaxtj/cxtj-nobody.png'>");
				}else{
					$("#chaxtj-hclb-img").html("");
					$("#chaxtj-haocaimxlbnr").css("display","none");
					$("#chaxtj-haocaihzlbnr").css("display","block");
				}
			}
		});
	function createHaocaiHz(name) {
		$('#tb-haocaihz').html('');
	  var container = $('#pagination-'+name);
	  container.pagination({
	    dataSource:'../chaxuntjylhc/getYlhcHzByPage?kaishisj='+kaishisj+'&jieshusj='+jieshusj+'&mingcheng='+mingcheng+'&yewulx='+yewulx,
	    locator: 'entities',
	    pageSize: 12,
	    totalNumber: totalNumber,
		showNavigator: true,
   	    formatNavigator: '共 <%= totalNumber %> 条',
	    callback: function(data, pagination) {
	    	if(data!='') {
				var content = '';
				$(data).each(function(index,e){
					if(this.xiaoshousl!=null&&this.danwei!=null){
						var shuliang = this.xiaoshousl + this.danwei;
					}else if(this.xiaoshousl!=null){
						var shuliang=this.xiaoshousl;
					}
					var yewulx = this.laiyuan;
					if(yewulx==1) {
						yewulx = '微诊疗';
					}else if(yewulx==2) {
						yewulx = '售药';
					}else if(yewulx==3) {
						yewulx = '常规门诊';
					} else if(yewulx == 4) {
						yewulx = '库存调整';
					}
					//厂商处理
					var changshang;
					if(this.changshang!=null){
						changshang=this.changshang;
					}else{
						changshang='';
					}
					//规格处理
					var guige;
					if(this.guige!=null){
						guige=this.guige;
					}else{
						guige='';
					}
					content += '<tr>'+
	                '<td width="5%">'+(((pagination.pageNumber-1)*pagination.pageSize)+index+1)+'</td>'+
	                '<td width="20%" class="left">'+this.mingcheng+'</td>'+
	                '<td width="15%">'+guige+'</td>'+
	                '<td width="20%" class="left">'+changshang+'</td>'+
	                '<td width="12%">'+yewulx+'</td>'+
	                '<td width="13%">'+shuliang+'</td>'+
	                '<td width="15%" class="right">'+parseFloat(this.xiaoshouje).toFixed(2)+'</td>'+
	                '</tr>';
				});
				$('#tb-haocaihz').html(content);
	    	}
	    }
	  });
	  return container;
	}
	createHaocaiHz('fenyylhchz');
	if(totalNumber <= 12) {
		$('#pagination-fenyylhchz').css('display','none');
	}else{
		$('#pagination-fenyylhchz').css('display','block');
	}
}